﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eTruck.Helpers;
namespace eTruck.Views.FormTypes
{
    public partial class Form : System.Web.UI.Page
    {
        protected string returnValue = string.Empty;

        BLL.FormTypes bll = new BLL.FormTypes();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = System.Web.HttpUtility.UrlDecode(Request["Type"]);
            if (type == "Save")
            {
                Save();
            }
            else
            {
                Exists();
            }
        }

        private void Save()
        {
            Model.FormTypes model = new Model.FormTypes();

            string FormTypeID = System.Web.HttpUtility.UrlDecode(Request["FormTypeID"]);
            model.FormType = System.Web.HttpUtility.UrlDecode(Request["FormType"]);
            model.Desc = System.Web.HttpUtility.UrlDecode(Request["Desc"]).ToString();
           

           

            try
            {
                bool result = false;
                if (FormTypeID.Length == 0)
                {
                    //create new User
                    result = bll.Add(model);
                }
                else
                {
                    //update User Info...
                    model.FormTypeID = Convert.ToInt32(FormTypeID);
                    result = bll.Update(model);
                }
                if (result)
                {
                    returnValue = "True";
                }
                else
                {
                    returnValue = "False";
                }
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
        }

        private void Exists()
        {
            string FormType= System.Web.HttpUtility.UrlDecode(Request["FormType"]);

            DataSet ds = bll.GetList("FormType='" + FormType + "'");
            if (ds == null)
            {
                returnValue = "False";
            }
            else if (ds.Tables.Count == 0)
            {
                returnValue = "False";
            }
            else if (ds.Tables[0].Rows.Count == 0)
            {
                returnValue = "False";
            }
            else
            {
                returnValue = "True";
            }

        }
    }
}

